package team.knowers.firm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.knowers.firm.common.database.BaseDO;
import team.knowers.firm.dao.entity.FlightDO;
import team.knowers.firm.dao.mapper.BookingMapper;
import team.knowers.firm.dao.mapper.FlightMapper;
import team.knowers.firm.dao.mapper.PaymentMapper;
import team.knowers.firm.dto.req.AddFlightReqDTO;
import team.knowers.firm.dto.req.PageFlightInfoReqDTO;
import team.knowers.firm.dto.resp.FlightInfoRespDTO;
import team.knowers.firm.service.FlightService;
import team.knowers.firm.service.NoticeService;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FlightServiceImpl extends ServiceImpl<FlightMapper,FlightDO> implements FlightService {
    private final FlightMapper flightMapper;
    private final NoticeService noticeService;
    @Override
    public Boolean addFlight(AddFlightReqDTO req) {
        FlightDO one = FlightDO.builder()
                .flightNo(req.getFlightNo())
                .ecoPrice(req.getEcoPrice())
                .ecoSeats(req.getEcoSeats())
                .endPlace(req.getEndPlace())
                .startPlace(req.getStartPlace())
                .endTime(req.getEndTime())
                .headPrice(req.getHeadPrice())
                .headSeats(req.getHeadSeats())
                .status(req.getStatus())
                .nowEcoSeats(req.getEcoSeats())
                .nowHeadSeats(req.getHeadSeats())
                .startTime(req.getStartTime())
                .build();
        return flightMapper.insert(one) > 0;
    }

    @Override
    public IPage<FlightInfoRespDTO> getAllFlightInfo(PageFlightInfoReqDTO req) {
        LambdaQueryWrapper<FlightDO> flightDOLambdaQueryWrapper = Wrappers.lambdaQuery(FlightDO.class).eq(BaseDO::getDelFlag, 0).orderByAsc(FlightDO::getStartTime);
        IPage<FlightDO> page = flightMapper.selectPage(req, flightDOLambdaQueryWrapper);
        return page.convert(one-> BeanUtil.toBean(one, FlightInfoRespDTO.class));
    }

    @Override
    public FlightInfoRespDTO getFlightInfo(Integer id) {
        return BeanUtil.toBean(flightMapper.selectById(id), FlightInfoRespDTO.class);
    }

    @Override
    @Transactional
    public Boolean updateFlight(FlightDO req) {
        noticeService.addNoticeUpdateFlight(req);
        return flightMapper.updateById(req) > 0;
    }

    @Override
    public Boolean deleteFlight(Integer id) {
        noticeService.addNoticeCancelFlight(id);
        LambdaUpdateWrapper<FlightDO> set = Wrappers.lambdaUpdate(FlightDO.class)
                .eq(FlightDO::getDelFlag, 0)
                .eq(FlightDO::getId, id)
                .set(FlightDO::getStatus, "已取消");
        return flightMapper.update(null, set) > 0;
    }

    @Override
    public List<FlightInfoRespDTO> getFlightInfoByFlightNo(String flightNo) {
        List<FlightDO> flightDOS = flightMapper.selectList(Wrappers.lambdaQuery(FlightDO.class)
                .eq(FlightDO::getFlightNo, flightNo)
                .eq(BaseDO::getDelFlag, 0));
        return flightDOS.stream().map(one-> BeanUtil.toBean(one, FlightInfoRespDTO.class)).collect(Collectors.toList());
    }
}
